package com.zggk.framework.service.score;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.platform.common.bean.Message;
import com.platform.common.utils.JsonUtils;
import com.platform.framework.service.BaseServiceImpl;
import com.zggk.framework.dao.score.GjGradeRepository;
import com.zggk.framework.entity.score.GjGrade;

/**
 * @Description 
 * @author 张立增[zhanglizeng] Tel：18860126570
 * @email 1472052711@qq.com
 * @date 2020-02-26 11:30:46
 */
 @Service
public class GjGradeService extends BaseServiceImpl {

    @Autowired
    GjGradeRepository gjGradeRepository;

    /**
     * @Description 得到评分记录
     * @author 张立增
     * @Date 2020年2月29日 下午9:00:12
     */
	public String loadGradeData(String id, String zqCode, String roadType) {
		try {
			String hql = "select * from score.gj_grade where district_code = ? and highway_grade = ? and item_code = ?";
			List<GjGrade> list = this.getDao().findBySql(hql,new Object[] {zqCode,roadType,id},GjGrade.class);
			return JsonUtils.dealJson(list);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

    /**
     * @Description  保存或更新数据
     * @author 张立增
     * @Date 2020年2月29日 下午11:50:32
     */
	public String savaOrUpdateGrade(String id, String zqCode, String roadType, String checkScore, String checkDate) {
		try {
			String hql = "select * from score.gj_grade where district_code = ? and highway_grade = ? and item_code = ?";
			List<GjGrade> list = this.getDao().findBySql(hql,new Object[] {zqCode,roadType,id},GjGrade.class);
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			if(null != list && list.size() > 0) {
				GjGrade gjGrade = list.get(0);
				gjGrade.setCheckScore(new BigDecimal(checkScore));
				gjGrade.setCheckDate(new Date(checkDate));
				gjGradeRepository.save(gjGrade);
			}else {
				GjGrade gjGrade = new GjGrade();
				gjGrade.setId(getUUID());
				gjGrade.setDistrictCode(zqCode);
				gjGrade.setHighwayGrade(roadType);
				gjGrade.setItemCode(id);
				gjGrade.setCheckScore(new BigDecimal(checkScore));
				gjGrade.setCheckDate(new Date(checkDate));  
				gjGradeRepository.save(gjGrade);
			}
			return new Message(Message.OK,"保存成功").toJson();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


 
   
}

